// 104.二叉树的最大深度
package LeetcodeHot100;

import java.util.Deque;
import java.util.LinkedList;



public class Solution104 {
    public int maxDepth(TreeNode root) {
        if (root == null)
            return 0;
        int maxDepth = 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size(); // 这里要提前记录size，否则在遍历过程中其值会改变
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                if (node.left != null)
                    deque.offer(node.left);
                if (node.right != null)
                    deque.offer(node.right);
            }
            maxDepth++;
        }
        return maxDepth;
    }
}
